<?php
namespace app\admin\controller;

use think\Db;
use think\Request;
class Adv extends Base
{
	/**
	 * 广告位列表
	 * @return [type] [description]
	 */
	function index(){
		$keywords = Request()->get('keywords');
		$sort = Request()->get('adp_id')?Request()->get('adp_id'):"adp_id asc";
		$this->assign('sort',$sort);
		$query = Db::name('adposition');
		if ($keywords) {
			$query->where(" adp_name like '%{$keywords}%' or adp_key like '%{$keywords}%' ");
		}
		$list=$query->order($sort)->select();
		$this->assign('list',$list);
		return $this->fetch();
	}

	/**
	 * 添加广告位
	 * @return [type] [description]
	 */
	function addadp(){
		$adp_id = Request()->get('adp_id');
		$data = Request()->only('adp_name,adp_key,adp_desc','post');
		//查找广告标识是否已经存在
		$has = Db::name('adposition')->where(['adp_key'=>$data['adp_key']])->find();
		if ($has) {
			$this->error('广告标识已经存在');
		}
		$re = Db::name('adposition')->insert($data);
		if ($re) {
			$data['adp_id']= Db::name('adposition')->getLastInsID();
			$this->success('新增成功','',$data);
		}else{
			$this->error('新增失败');
		}
	}

	/**
	 * 编辑广告位
	 * @return [type] [description]
	 */
	function editadp()
	{
		$data = json_decode(file_get_contents('php://input'),true);
		foreach ($data as $k => $v) {
			empty($v['adp_key']) && $this->error('请填写广告标识');
			//查找广告标识是否已经存在
			$has = Db::name('adposition')->where(['adp_key'=>$v['adp_key'],'adp_id'=>['neq',$v['adp_id']]])->find();
			$has && $this->error('广告标识已经存在');

			$re = Db::name('adposition')->where(['adp_id'=>$v['adp_id']])->update($v);
			if ($re === false) {
				$this->error('保存失败');
			}
		}
		$this->success('保存成功','refresh');
	}

	/**
	 * 删除广告位和广告
	 * @return [type] [description]
	 */
	function deladp(){

		$ids = Request()->param('ids');
		if (empty($ids)) {
			$this->error('参数错误');
		}
		//删除下面的广告
		Db::name('adv')->where(['adp_id'=>['in',$ids]])->delete();
		Db::name('adposition')->where(['adp_id'=>['in',$ids]])->delete();
		$this->success('删除成功');
	}


	/**
	 * 广告列表
	 * @return [type] [description]
	 */
	function advlist(){
		$adp_id = Request()->param('adp_id');
		$sort = Request()->get('adv_id_sort')?Request()->get('adv_id_sort'):"adv_id asc";
		$this->assign('sort',$sort);
		if (empty($adp_id)) {
			$this->error('参数错误');
		}

		$keywords = Request()->get('keywords');
		$query = Db::name('adv');
		if ($keywords) {
			$query->where("adv_name like '%{$keywords}%'");
		}

		$list = $query->where(['adp_id'=>$adp_id])->order('adv_sort asc,'.$sort)->select();

		$this->assign('list',$list);
		$this->assign('adp_id',$adp_id);
		
		return $this->fetch();
	}

	/**
	 * 添加广告
	 * @return [type] [description]
	 */
	function addadv(){
		if (Request()->isPost()) {
			$adp_id=Request()->param('adp_id');
			if (empty($adp_id)) {
				$this->error('参数错误');
			}
			
			$param = Request()->param();
			$data = [
				'adp_id'        => $adp_id,
				'adv_name'      => $param['adv_name'],
				'adv_desc'      => $param['adv_desc'],
				'adv_status'    => $param['adv_status'],
				'start_time'    => $param['start_time'],
				'end_time'      => $param['end_time'],
				'adv_img'       => $param['adv_img'],
				'adv_thumb'		=> $param['adv_thumb'],
				'adv_sort'      => $param['adv_sort'],
				'realtion_type' => $param['realtion_type']
			];
			if ($param['realtion_type'] == 'brand') {
				$data['realtion_content'] = implode(',',$param['brands']);
			}elseif ($param['realtion_type'] == 'cate') {
				$data['realtion_content'] = implode(',',$param['cates']);
			}elseif ($param['realtion_type'] == 'goods') {
				$data['realtion_content'] = implode(',',$param['goods']);
			}else{
				$data['realtion_content'] = null;
			}

			$re = Db::name('adv')->insert($data);
			if ($re) {
				$data['adv_id']= Db::name('adv')->getLastInsID();
				$this->success('新增成功','back');
			}else{
				$this->error('新增失败');
			}
		}else{
			$this->assign('adp_id',Request()->param('adp_id'));
			return $this->fetch();
		}
	}

	/**
	 * 删除广告
	 * @return [type] [description]
	 */
	function deladv(){
		$ids = Request()->param('ids');
		if (empty($ids)) {
			$this->error('参数错误');
		}
		//删除下面的广告
		Db::name('adv')->where(['adv_id'=>['in',$ids]])->delete();
		$this->success('删除成功');
	}

	/**
	 * 编辑广告
	 * @return [type] [description]
	 */
	function editadv(){
		if (Request()->isPost()) {

			$param = Request()->param();
			if (empty($param['adv_id'])) {
				$this->error('参数错误');
			}
			
			$data = [
				'adv_name'      => $param['adv_name'],
				'adv_desc'      => $param['adv_desc'],
				'adv_status'    => $param['adv_status'],
				'start_time'    => $param['start_time'],
				'end_time'      => $param['end_time'],
				'adv_img'       => $param['adv_img'],
				'adv_thumb'		=> $param['adv_thumb'],
				'adv_sort'      => $param['adv_sort'],
				'realtion_type' => $param['realtion_type']
			];
			if ($param['realtion_type'] == 'brand') {
				$data['realtion_content'] = implode(',',$param['brands']);
			}elseif ($param['realtion_type'] == 'cate') {
				$data['realtion_content'] = implode(',',$param['cates']);
			}elseif ($param['realtion_type'] == 'goods') {
				$data['realtion_content'] = implode(',',$param['goods']);
			}else{
				$data['realtion_content'] = null;
			}

			$re = Db::name('adv')->where(['adv_id'=>$param['adv_id']])->update($data);
			if ($re!==false) {
				$this->success('操作成功','back');
			}else{
				$this->error('操作失败');
			}
		}else{
			$adv_id=Request()->param('adv_id');
			if (empty($adv_id)) {
				$this->error('参数错误');
			}
			$info=Db::name('adv')->where(['adv_id'=>$adv_id])->find();
			$data['info'] = $info;

			return $this->fetch('editadv',$data);
		}
	}

	/**
	 * 添加商品
	 */
	public function addGoods()
	{
		$param = Request()->param();
		$ids = $param['ids']?:0;
		$list = Db::table('sdzx_goods')->field('g.*,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,sum(sval.sales) as sales,max(sval.status) as status,max(sval.is_recommend) as is_recommend')
		->alias('g')->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id')
		->group('g.goods_id')->where("g.goods_id in ({$ids})")->select();
		if (Request()->isAjax()) {
			$this->success('success','',$list);
		}else{
			return $this->fetch('addGoods',['list'=>$list]);
		}
	}

	public function goodsList()
	{
		$keywords = Request()->get('keywords');
		$query = Db::table('sdzx_goods')->field('g.*,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,sum(sval.sales) as sales,max(sval.status) as status,max(sval.is_recommend) as is_recommend')
		->alias('g')->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id')
		->group('g.goods_id');
		if ($keywords) {
			$query->where(" goods_name like '%{$keywords}%' ");
		}
		$list=$query->order('sort desc, goods_id asc')->paginate('',false,['query'=>Request()->param()]);

		return $this->fetch('goodsList',['list'=>$list]);
	}

	public function quickEdit()
	{
		$param = Request()->param();
		$data[$param['field']] = $param['value'];
		$re =  Db::name('adv')->where(['adv_id'=>$param['id']])->update($data);
		if ($re!==false) {
			$this->success('操作成功','refresh');
		}else{
			$this->error('操作失败');
		}
	}
}